package com.shop.prj.dao.Impl;

import com.shop.prj.bean.BookStyle;
import com.shop.prj.bean.OrderItem;
import com.shop.prj.bean.ShopCar;
import com.shop.prj.dao.ShopCarDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ShopCarDaoImpl implements ShopCarDao {
	@Override
	public int insert(ShopCar shopCar) {
		String sql = "INSERT INTO `shopCar`(`user_id`,`book_id`,`book_num`,`book_price`,`book_name`) VALUES (?,?,?,?,?)";
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = JDBCUtils.getConnection();
		try {
			preparedStatement = connection.prepareStatement(sql);

			preparedStatement.setInt(1, shopCar.getUserId());
			preparedStatement.setInt(2, shopCar.getBookId());
			preparedStatement.setInt(3, shopCar.getBookNum());
			preparedStatement.setDouble(4, shopCar.getBookPrice());
			preparedStatement.setString(5, shopCar.getBookName());

			result = preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();

			}
		}
		return result;
	}

	@Override
	public int update(ShopCar shopCar) {
		String sql = "UPDATE `shopcar` SET `user_id`=?,`book_id`=?,`book_num`=?,`book_price`=? ,`book_name`=? WHERE  `shopcar_id`=?";
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = JDBCUtils.getConnection();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, shopCar.getUserId());
			preparedStatement.setInt(2, shopCar.getBookId());
			preparedStatement.setInt(3, shopCar.getBookNum());
			preparedStatement.setDouble(4, shopCar.getBookPrice());
			preparedStatement.setString(5, shopCar.getBookName());
			preparedStatement.setInt(6, shopCar.getShopCarId());
			result = preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();

			}
		}
		return result;
	}

	@Override
	public int delete(Integer id) {
		String sql = "DELETE  FROM `shopCar` WHERE `shopCar_id`=?";
		Connection connection = JDBCUtils.getConnection();
		PreparedStatement preparedStatement = null;
		int result = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			result = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public List<ShopCar> queryAll() {
		String sql = "SELECT * FROM `shopCar`";

		Connection connection = JDBCUtils.getConnection();
		Statement statement = null;
		ResultSet resultSet = null;
		List<ShopCar> list = new ArrayList<ShopCar>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				ShopCar shopCar = new ShopCar();
				shopCar.setShopCarId(resultSet.getInt(1));
				shopCar.setUserId(resultSet.getInt(2));
				shopCar.setBookId(resultSet.getInt(3));
				shopCar.setBookNum(resultSet.getInt(4));
				shopCar.setBookPrice(resultSet.getDouble(5));
				shopCar.setBookName(resultSet.getString(6));
				list.add(shopCar);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public ShopCar queryshopCar(String bookName) {
		String sql = "SELECT * FROM `shopCar` WHERE `book_name`=?";

		Connection connection = JDBCUtils.getConnection();
		PreparedStatement preparedStatement = null;

		ResultSet resultSet = null;
		ShopCar shopCar = null;

		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, bookName);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				shopCar = new ShopCar();
				shopCar.setShopCarId(resultSet.getInt(1));
				shopCar.setUserId(resultSet.getInt(2));
				shopCar.setBookId(resultSet.getInt(3));
				shopCar.setBookNum(resultSet.getInt(4));
				shopCar.setBookPrice(resultSet.getDouble(5));
				shopCar.setBookName(resultSet.getString(6));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		return shopCar;
	}
}
